<?php

namespace App\Admin\Controllers;

use App\Models\Waybill;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use App\Admin\Actions\Waybill\ImportWaybill;
use App\Admin\Actions\Waybill\ClearAll;
use App\Admin\Actions\Waybill\Recosting;
use App\Admin\Actions\Waybill\BatchRecost;
use App\Admin\Extensions\WaybillExporter;

class WaybillController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '运单列表';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Waybill());

        $grid->column('id', __('编号'))->sortable();
        $grid->column('order_no', __('淘宝订单'));
        $grid->column('express_no', __('运单'));
        $grid->column('goods_name', __('品名'));
        $grid->column('goods_bn', __('商品编号'));
        $grid->column('part_bn', __('料号'));
        $grid->column('goods_num', __('商品数量'))->sortable();
        $grid->column('consignee_name', __('收件人'));
        $grid->column('consignee_address', __('收件人地址'));
        $grid->column('supplier_name', __('供应商账号'));
        $grid->column('seller_name', __('店铺卖家'));
        $grid->column('declare_status', __('清单申报状态'))->display(function ($declare_status){
            $arr = ['0' => '待申报', '1' =>'已申报'];
            return $arr[$declare_status] ?? '-';
        })->label(['0' => 'warning', '1'=>'success', '-'=> 'default']);
        $grid->column('status', __('订单状态'))->display(function ($status){
            $arr = ['0' => '未计算', '1' =>'已计算'];
            return $arr[$status] ?? '-';
        })->label(['0' => 'warning', '1'=>'success', '-'=> 'default'])->sortable();
        $grid->column('platform_delivery_status', __('平台发货状态'))->display(function ($platform_delivery_status){
            $arr = ['0' => '未发货', '1' => '已发货', '2' => '订单取消', '3' => '下单中,等待回执'];
            return $arr[$platform_delivery_status] ?? '-';
        })->sortable();
        $grid->column('warehouse_delivery_status', __('仓库下单状态'))->display(function ($warehouse_delivery_status){
            $arr = ['0'=>'订单取消成功', '1'=>'下单中,等待回执'];
            return $arr[$warehouse_delivery_status] ?? '-';
        })->sortable();
        $grid->column('clearance_type', __('报关类型'));
        $grid->column('order_amount', __('订单总价'))->sortable();
        $grid->column('goods_price', __('合作价'))->sortable();
        $grid->column('cost_amount', __('成本价'))->sortable();
        $grid->column('profit_amount', __('利润'))->sortable();
        $grid->column('created_at', __('创建时间'))->sortable();
        $grid->column('updated_at', __('更新时间'))->sortable();

        $grid->filter(function($filter) {

            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            // 在这里添加字段过滤器
            $filter->like('order_no', '淘宝订单');
            $filter->like('express_no', '运单号');
            $filter->like('goods_bn', '商品编号');
            $filter->like('consignee_name', '收件人');
            $filter->like('goods_name', '商品名称');
        });

        $grid->tools(function (Grid\Tools $tools) {
            $tools->append(new ImportWaybill());
            $tools->append(new Recosting());
            $tools->append(new ClearAll());
        });

        $grid->batchActions(function ($batch) {
            $batch->add(new BatchRecost());
        });

        $excel = new WaybillExporter();
        $grid->exporter($excel);

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Waybill::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('order_no', __('Order no'));
        $show->field('express_no', __('Express no'));
        $show->field('goods_name', __('Goods name'));
        $show->field('goods_bn', __('Goods bn'));
        $show->field('part_bn', __('Part bn'));
        $show->field('goods_num', __('Goods num'));
        $show->field('consignee_name', __('Consignee name'));
        $show->field('consignee_address', __('Consignee address'));
        $show->field('supplier_name', __('Supplier name'));
        $show->field('seller_name', __('Seller name'));
        $show->field('order_amount', __('Order amount'));
        $show->field('declare_status', __('Declare status'));
        $show->field('status', __('Status'));
        $show->field('platform_delivery_status', __('Platform delivery status'));
        $show->field('warehouse_delivery_status', __('Warehouse delivery status'));
        $show->field('clearance_type', __('Clearance type'));
        $show->field('goods_price', __('Goods price'));
        $show->field('cost_amount', __('Cost amount'));
        $show->field('profit_amount', __('Profit amount'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Waybill());

        $form->text('order_no', __('Order no'));
        $form->text('express_no', __('Express no'));
        $form->text('goods_name', __('Goods name'));
        $form->text('goods_bn', __('Goods bn'));
        $form->text('part_bn', __('Part bn'));
        $form->number('goods_num', __('Goods num'));
        $form->text('consignee_name', __('Consignee name'));
        $form->textarea('consignee_address', __('Consignee address'));
        $form->text('supplier_name', __('Supplier name'));
        $form->text('seller_name', __('Seller name'));
        $form->decimal('order_amount', __('Order amount'))->default(0.00);
        $form->switch('declare_status', __('Declare status'));
        $form->switch('status', __('Status'));
        $form->switch('platform_delivery_status', __('Platform delivery status'));
        $form->switch('warehouse_delivery_status', __('Warehouse delivery status'));
        $form->text('clearance_type', __('Clearance type'));
        $form->decimal('goods_price', __('Goods price'))->default(0.00);
        $form->decimal('cost_amount', __('Cost amount'))->default(0.00);
        $form->decimal('profit_amount', __('Profit amount'))->default(0.00);

        return $form;
    }
}
